/*
 * Order.java
 *
 * Created on 12 novembre 2007, 15:56
 */
package com.reimscopie ;

import java.util.* ;


/**
 * This is an order, already commited.
 */
public class Order extends Value {
  
  // the pruchased products
  private Purchase[] purchases ;
  
  // commitment date
  private Date date ;
  
  // shipping costs
  private float shipping ;
  
  // order status
  private boolean valid ;
  
  
  /**
   * Create a new <code>Order</code> instance with all attributes. This
   * object has no image in the database.
   *
   * @param purchases The purchased products.
   * @param date The date of the order commitment.
   * @param shipping The shipping costs for this command (money amount).
   * @param valid The order status.
   */
  public Order(Purchase[] purchases,Date date,float shipping,boolean valid) {
    
    super() ;
    
    this.purchases = purchases ;
    this.date      = date ;
    this.shipping  = shipping ;
    this.valid     = valid ;
    
  }
  
  
  /**
   * Create a new <code>Order</code> instance with an image in the database.
   *
   * @param id The order id in the database.
   * @param purchases The purchased products.
   * @param date The date of the order commitment.
   * @param shipping The shipping costs for this command (money amount).
   * @param valid The order status.
   */
  public Order(long id,Purchase[] purchases,Date date,float shipping,
      boolean valid) {
    
    super(id) ;
    
    this.purchases = purchases ;
    this.date      = date ;
    this.shipping  = shipping ;
    this.valid     = valid ;
    
  }
  
  
  /**
   * Return the commitment date for this order.
   *
   * @return The commitment date.
   */
  public Date getCommitmentDate() {
    return date ;
  }
  
  
  /**
   * Return the shipping costs for this order (money amount).
   *
   * @return The shipping costs.
   */
  public float getShippingCosts() {
    return shipping ;
  }
  
  
  /**
   * Return the total cost of the order.
   *
   * @return The total cost.
   */
  public float getTotalCost() {
    
    float coast = getShippingCosts() ;
    
    for (Purchase purchase : purchases)
      coast += purchase.getTotalCost() ;
    
    return coast ;
  }
  
  
  /**
   * Return the purchases of the order.
   *
   * @return The purchases list.
   */
  public Purchase[] getPurchases() {
    return purchases ;
  }
  
  
  /**
   * Return the status of this order.
   *
   * @return <code>true</code> is the order is valid, <code>false</code>
   * elsewhere.
   */
  public boolean  isValid() {
    return valid ;
  }
  
  
  /**
   * Create a new map with all attributes of this value.
   *
   * @return A string-string map with the named attributes.
   */
  @Override
  public Map<String,String> toMap() {
    
    Map<String,String> map = new LinkedHashMap<String,String>() ;
    
    map.put("date",date.toString()) ;
    map.put("shipping",String.valueOf(shipping)) ;
    map.put("valid",String.valueOf(valid)) ;
    
    return map ;
  }
  
}
